<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="85px">
      <el-form-item label="盘点单编号" prop="sn">
        <el-input
          v-model="queryParams.sn"
          placeholder="请输入库存盘点单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库名称" prop="warehouseName">
        <el-input
          v-model="queryParams.warehouseName"
          placeholder="请输入仓库名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库区名称" prop="locationName">
        <el-input
          v-model="queryParams.locationName"
          placeholder="请输入库区名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库位名称" prop="areaName">
        <el-input
          v-model="queryParams.areaName"
          placeholder="请输入库位名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="盘点人" prop="inventoryPerson">
        <el-input
          v-model="queryParams.inventoryPerson"
          placeholder="请输入盘点人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
<!--        <el-input
          v-model="queryParams.status"
          placeholder="请输入状态"
          clearable
          @keyup.enter.native="handleQuery"
        />-->
        <el-select v-model="queryParams.status" placeholder="请选择盘点状态" clearable>
          <el-option label="盘点中" value="盘点中"></el-option>
          <el-option label="已提交" value="已提交"></el-option>
        </el-select>

      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mes/wm:pandian:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mes/wm:pandian:remove']"
        >删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['mes/wm:pandian:export']"
        >导出</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="pandianList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="主键ID" align="center" prop="id" />-->
      <el-table-column label="库存盘点单编号" align="center" prop="sn" >
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleView(scope.row)"
          >{{scope.row.sn}}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="仓库名称" align="center" prop="warehouseName" />
      <el-table-column label="库区名称" align="center" prop="locationName" />
      <el-table-column label="库位名称" align="center" prop="areaName" />

      <el-table-column label="盘点人" align="center" prop="inventoryPerson" />
      <el-table-column label="盘点时间" align="center" prop="inventoryTime" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.inventoryTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" >
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.status == '已提交'">{{scope.row.status}}</el-tag>
          <el-tag type="danger" v-if="scope.row.status == '盘点中'">{{scope.row.status}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="盘点说明" align="center" prop="inventoryDesc" />
<!--      <el-table-column label="审核时间" align="center" prop="auditTime" />
      <el-table-column label="审核人名称" align="center" prop="auditPersonName" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-info"
            @click="tijiao(scope.row)"
            v-hasPermi="['mes/wm:pandian:edit']"
          >查看/提交</el-button>
          <el-button v-if="scope.row.status !== '已提交'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mes/wm:pandian:edit']"
          >修改</el-button>
          <el-button v-if="scope.row.status !== '已提交'"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mes/wm:pandian:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改库存盘点对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="库存盘点单编号" prop="sn">
          <el-input v-model="form.sn" placeholder="请输入库存盘点单编号" />
        </el-form-item>
        <el-form-item label="仓库ID" prop="warehouseId">
          <el-input v-model="form.warehouseId" placeholder="请输入仓库ID" />
        </el-form-item>
        <el-form-item label="仓库名称" prop="warehouseName">
          <el-input v-model="form.warehouseName" placeholder="请输入仓库名称" />
        </el-form-item>
        <el-form-item label="库区ID" prop="locationId">
          <el-input v-model="form.locationId" placeholder="请输入库区ID" />
        </el-form-item>
        <el-form-item label="库区名称" prop="locationName">
          <el-input v-model="form.locationName" placeholder="请输入库区名称" />
        </el-form-item>
        <el-form-item label="库位ID" prop="areaId">
          <el-input v-model="form.areaId" placeholder="请输入库位ID" />
        </el-form-item>
        <el-form-item label="库位名称" prop="areaName">
          <el-input v-model="form.areaName" placeholder="请输入库位名称" />
        </el-form-item>
        <el-form-item label="盘点人ID" prop="inventoryPersonId">
          <el-input v-model="form.inventoryPersonId" placeholder="请输入盘点人ID" />
        </el-form-item>
        <el-form-item label="盘点人" prop="inventoryPerson">
          <el-input v-model="form.inventoryPerson" placeholder="请输入盘点人" />
        </el-form-item>
        <el-form-item label="盘点时间" prop="inventoryTime">
          <el-input v-model="form.inventoryTime" placeholder="请输入盘点时间" />
        </el-form-item>
        <el-form-item label="盘点说明" prop="inventoryDesc">
          <el-input v-model="form.inventoryDesc" placeholder="请输入盘点说明" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-input v-model="form.status" placeholder="请输入状态" />
        </el-form-item>
        <el-form-item label="审核驳回原因" prop="rejectReason">
          <el-input v-model="form.rejectReason" placeholder="请输入审核驳回原因" />
        </el-form-item>
        <el-form-item label="审核时间" prop="auditTime">
          <el-input v-model="form.auditTime" placeholder="请输入审核时间" />
        </el-form-item>
        <el-form-item label="审核人ID" prop="auditPersonId">
          <el-input v-model="form.auditPersonId" placeholder="请输入审核人ID" />
        </el-form-item>
        <el-form-item label="审核人名称" prop="auditPersonName">
          <el-input v-model="form.auditPersonName" placeholder="请输入审核人名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPandian, getPandian, delPandian, addPandian, updatePandian } from "@/api/mes/wm/pandian";

export default {
  name: "Pandian",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      //选中盘点单号
      pdno:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 库存盘点表格数据
      pandianList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sn: null,        warehouseName: null,        locationName: null,        areaName: null,        inventoryPerson: null,        status: null,      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  activated() {
    if (localStorage.getItem("IndexRefresh") == "true") {
      localStorage.setItem("IndexRefresh", false);
      this.getList();
    }
  },

  created() {
    this.getList();
  },
  methods: {
    handleView(row){
      this.$router.push({path: '/detailPandian',query: {id: row.id}});
    },
    /** 查询库存盘点列表 */
    getList() {
      this.loading = true;
      listPandian(this.queryParams).then(response => {
        this.pandianList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,        sn: null,        warehouseId: null,        warehouseName: null,        locationId: null,        locationName: null,        areaId: null,        areaName: null,        inventoryPersonId: null,        inventoryPerson: null,        inventoryTime: null,        inventoryDesc: null,        status: null,        rejectReason: null,        auditTime: null,        auditPersonId: null,        auditPersonName: null,        createBy: null,        createTime: null,        updateBy: null,        updateTime: null      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.pdno= selection.map(item => item.sn)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({path: '/addPandian'});

    },
    /** 修改按钮操作 */
    handleUpdate(row) {
        this.$router.push({path: '/editPandian',query: {id: row.id}});

    },
    tijiao(row){
      this.$router.push({path: '/tijiaoPandian',query: {id: row.id}});
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePandian(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPandian(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除库存盘点编号为"' + this.pdno + '"的数据项？').then(function() {
        return delPandian(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mes/wm/pandian/export', {
        ...this.queryParams
      }, `pandian_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
